package com.lhj.xxsc.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.xxsc.common.exception.BizCodeEnume;
import com.lhj.xxsc.member.exception.PhoneExsitException;
import com.lhj.xxsc.member.exception.UserNameExistException;
import com.lhj.xxsc.member.feign.CouponFeignService;
import com.lhj.xxsc.member.to.SocialUserTo;
import com.lhj.xxsc.member.to.UserLoginTo;
import com.lhj.xxsc.member.to.UserRegistTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lhj.xxsc.member.entity.MemberEntity;
import com.lhj.xxsc.member.service.MemberService;
import com.xxsc.common.utils.PageUtils;
import com.xxsc.common.utils.R;


/**
 * 会员
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2019-10-08 09:47:05
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
	@Autowired
	private MemberService memberService;

	@Autowired
	CouponFeignService couponFeignService;

	@RequestMapping("/coupons")
	public R test() {
		MemberEntity memberEntity = new MemberEntity();
		memberEntity.setNickname("张三");

		R membercoupons = couponFeignService.membercoupons();
		return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons"));
	}


	/**
	 * 列表
	 */
	@RequestMapping("/list")
	//@RequiresPermissions("member:member:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = memberService.queryPage(params);

		return R.ok().put("page", page);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	//@RequiresPermissions("member:member:info")
	public R info(@PathVariable("id") Long id) {
		MemberEntity member = memberService.getById(id);

		return R.ok().put("member", member);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	//@RequiresPermissions("member:member:save")
	public R save(@RequestBody MemberEntity member) {
		memberService.save(member);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	//@RequiresPermissions("member:member:update")
	public R update(@RequestBody MemberEntity member) {
		memberService.updateById(member);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	//@RequiresPermissions("member:member:delete")
	public R delete(@RequestBody Long[] ids) {
		memberService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

	@PostMapping("/regist")
	public R regist(@RequestBody UserRegistTo to) {
		try {
			memberService.regist(to);
		} catch (UserNameExistException e) {
			return R.error(BizCodeEnume.USER_EXIST_EXECPTION.getCode(), BizCodeEnume.USER_EXIST_EXECPTION.getMsg());
		} catch (PhoneExsitException e) {
			return R.error(BizCodeEnume.PHONE_EXIST_EXECPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXECPTION.getMsg());
		} catch (Exception e) {
			return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(), BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
		}
		return R.ok();
	}

	@PostMapping("/oauth2/login")
	public R oauth2login(@RequestBody SocialUserTo to) throws Exception {
		MemberEntity entity = memberService.login(to);
		if (entity != null) {
			return R.ok().put("memberEntity",entity);
		} else {
			return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXECPTION.getCode(), BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXECPTION.getMsg());
		}
	}

	@PostMapping("/login")
	public R login(@RequestBody UserLoginTo to) {
		MemberEntity entity = memberService.login(to);
		if (entity != null) {
			// TODO 登录处理
			return R.ok().put("memberEntity",entity);
		} else {
			return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXECPTION.getCode(), BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXECPTION.getMsg());
		}
	}


}
